package dp;

/**
 * 45. 跳跃游戏 II
 */
public class P45 {
    public int jump(int[] nums) {
        if (nums.length <= 1) {
            return 0;
        }
        int len = nums.length;
        int target = len - 1;
        int[] dp = new int[len];
        dp[0] = 1;
        for (int i = 0; i < len; i++) {
            if (dp[i] != 0) {
                for (int j = 1; j <= nums[i]; j++) {//步长
                    int pos = i + j;
                    if (pos <= target) {
                        dp[pos] = dp[pos] == 0 ? dp[i] + 1 : Math.min(dp[i] + 1, dp[pos]);
                    }
                }
            }
        }
        return dp[target]-1;

    }

    public static void main(String[] args) {
        int jump = new P45().jump(new int[]{2,3,0,1,4});
        System.out.println(jump);
    }
}
